/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","msgServ", "ajaxServ", "formServ", "bizProto","btnServ"],
		function($,datagridServ, msgServ, ajaxServ, formServ, bizProto,btnServ) {

			'use strict';

			var _bizProto,_buttonConfig,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,window_btn,
				
				//业务表单对象
				$form
				
			var public_method = {				
				
				init:function(doc){
					private_method.init(doc);
				}
				
			}

			var private_method = {
				init : function(doc) {
					if (window.console) {
					console.info('log modules gets ready and been loaded.');
					}
					
					_bizProto = new bizProto({doc:doc,rootId:'panel-log',panelSelector:'div.well',subSystemCode:'sysman',moduleCode:'log'})
					_buttonConfig={
						"新增":private_method.add,
					    "编辑":private_method.edit,
						 "删除":private_method.del,
						 "保存":private_method.submit,
						 "取消":private_method.cancel									
					}
//					private_method.initPath();
					private_method.initElement(_bizProto.pageDoc)
				},
				
				//初始化路径
				initPath:function(){
					
				},				
				
				//初始化元素
				initElement:function(doc){					
					
					$dg = $("#log-dg");
					$grid = $dg.datagrid({
						height:_bizProto.pageHeight,
						url : _bizProto.queryDomainUrl						
					});
					
					// 编辑窗口初始化
					$edit_window = $('#log-win')
					
					$form = $($edit_window).find('form')					
					
					
					
					//初始化操作按钮
					var listTd = $(_bizProto.pageDoc).find('#tb-log td')
					
					// 编辑窗口初始化
					
					var editTd = $($edit_window).find('#tb-logEdit div')
					
					var _param = {
						bizcode:_bizProto.bizCode,
						rootPath:_bizProto.rootPath,
						containers:[listTd,editTd],
						callback:private_method.bindBtnEvent}
					btnServ.generateListAndEditBtn(_param);

				},
				
				//绑定按钮事件
				bindBtnEvent:function(){
					// 绑定按钮事件
					var btn = $(_bizProto.pageDoc).find('#tb-log a.function-btn')
					$(btn).unbind('click');
					$(btn).bind('click', private_method.function_btn_listenner)			
					
					window_btn = $($edit_window).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					
				},
				
				setComboValue:function(node,target){
		    		if(node!=null){		    			
		    			$(target).val(node.text);
		    		}
		    	},
				//功能按钮监听函数
				function_btn_listenner:function(e){
					
					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})
					
				},
				// 新增函数
				add : function() {
					$form = $($edit_window).find('form')
					$($form).form('clear');
					$edit_window.window('open');

				},
				// 编辑,flag 为form按钮可见性标志
				edit : function(flag) {
					var _param = {grid:$grid,window:$edit_window}
					datagridServ.edit(_param);
				},			
				
				// 删除
				del : function() {
					
					datagridServ.delRow({url:_bizProto.delUrl,target:$grid});
				},
				// 提交form
				submit : function() {
					
					datagridServ.save({form:$form,grid:$grid,url:_bizProto.saveUrl})					
				},				
				
				//
				cancel : function() {
					$($edit_window).window('close');
				}

			};
			
			return public_method;

		});